package leetcode.code0974;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int subarraysDivByK(int[] nums, int k) {
		int[] hash = new int[k];
		int sum = 0, ans = 0;
		for (int num : nums) {
			sum += num;
			int mod = sum % k;
			mod = (mod + k) % k;
			ans += hash[mod]++;
		}
		return ans + hash[0];
	}

	public int subarraysDivByK1(int[] nums, int k) {
		Map<Integer, Integer> hash = new HashMap<>();
		int sum = 0, ans = 0;
		for (int num : nums) {
			sum += num;
			int mod = sum % k;
			mod = (mod + k) % k;
			ans += hash.getOrDefault(mod, 0);
			hash.put(mod, hash.getOrDefault(mod, 0) + 1);
		}
		return ans + hash.getOrDefault(0, 0);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub
		H.compare(2, this.subarraysDivByK(H.str2array3("[-1,2,9]"), 2));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
